#include<bits/stdc++.h>
using namespace std;

class Solution {
public:
    int findMaxLength(vector<int>& nums) {
        unordered_map<int, int>mp;
        vector<int>pre_sum;
        int temp = 0;
        for(int i = 0; i < nums.size(); i++){
            temp += (nums[i] ? nums[i] : -1);
            pre_sum.push_back(temp);
            if(!mp.count(temp))mp[temp] = i;
        }
        int res = 0;
        for(int i = 0; i < pre_sum.size(); i++){
            if(!pre_sum[i])res = max(res, i + 1);
            else res = max(res, i - mp[pre_sum[i]]);
        }
        return res;
    }
};

/**
 * @brief 前缀和+hash
 * 
 */